#INCLUDE "PROTHEUS.CH"
#INCLUDE "APWEBEX.CH"
/*/{Protheus.doc} DTRWEB02

@descricao EXECUTA PAGINA DE AGENDAS

@author Peterson Scherwinski

@since 16/04/2014

/*/
User Function DTRWEB02
	Local cHtml	:= ""
	local oObj
	local cPagina	:= "DTRHTM03"//pagina a ser apresentada - lista das agendas
	local aAgendas:={} //montagem das agendas no formato colunas
	local oTmp

	Web Extended Init cHtml// START "init"
	
	//controle de acesso / timeout
	if type("HTTPSESSION->USERLOGIN") <> "A"
		HTTPSESSION->ALERT	:= "Necessario efetuar o login."
		RETURN( RedirPage('/U_DTRWEB01.apw'))
	endif
	
	HTTPSESSION->ALERT		:= ""
	HTTPSESSION->DATAS		:= {}
	HTTPSESSION->AGENDAS		:= {}
	HTTPSESSION->PARAMS		:= {}
	
	if LEN(HTTPSESSION->USERLOGIN)>0
		//Busca de agendas
		oObj := WSDTRWEBSR():New()
		oObj:CTOKEN 		:= "DATAROUTEWEB"

		//SE FOREM ENVIADAS AS DATAS, FAZ O FILTRO
		if !empty(HTTPPOST->dataDe).and.!empty(HTTPPOST->dataATE)
			dDataIni := ctod(HTTPPOST->dataDe)
			dDataFim := ctod(HTTPPOST->dataATE)
		else
			//mOtagem das datas corridas para a agenda
			nDiasRetro	:= 3
			dDataIni := date()-nDiasRetro
			dDataFim := date()+10
		endif
		
		HTTPSESSION->PARAMS		:= {dDataIni,dDataFim}

		dDataIniTmp	:= dDataIni
		nSomaDia := 0
		while dDataFim > dDataIniTmp 
			dDataIniTmp := dDataIniTmp+nSomaDia
			if nSomaDia == 0
				nSomaDia := 1
			endif
			aAdd(HTTPSESSION->DATAS,{dtoc(dDataIniTmp)})
		enddo

		IF HTTPSESSION->USERLOGIN[6]=="1"
			//Todos os recursos
			oObj:CRESOURCE := ""
		else
			oObj:CRESOURCE := HTTPSESSION->USERLOGIN[4]
		endif
		oObj:CDateStart	:= dtos(dDataIni)
		oObj:CDateEnd		:= dtos(dDataFim)

		//retorna as datas agendadas dos recuros
		IF oObj:GETSCHEDULE()
			oTmp := oObj:oWSGETSCHEDULERESULT:OWSDADOSAGENDA
			for nx:=1 to len(oTmp)
				
				//ADICIONA O RECURO NA LISTAGEM
				//|RECURSO,ARRAY DE DIAS|
				nPosRec := aScan(HTTPSESSION->AGENDAS,{|x| x[1]==oTmp[nx]:cRESOURCE})
				if nPosRec <= 0
					//MONTA ARRAY COM NOME DO RECURSO e ARRAY COM ESPACAO DE AGENDA PARA O PERIODO
					aAdd(HTTPSESSION->AGENDAS,{oTmp[nx]:cRESOURCE,oTmp[nx]:cRESONAME,array(len(HTTPSESSION->DATAS))})
					nPosRec := len(HTTPSESSION->AGENDAS)
					for nArray:=1 to len(HTTPSESSION->AGENDAS[nPosRec][3])
						HTTPSESSION->AGENDAS[nPosRec][3][nArray] := {}
					next
				endif

				//LOCALIZA POSICAO DA DATA AGENDADA COM A DATA NO CALENDARIO DA AGENDA
				nPosDia := aScan(HTTPSESSION->DATAS,{|x| x[1]==dtoc(stod(oTmp[nx]:cDATESCHE))})
				
				//CARREGA OS DADOS DA AGENDA NO DIA DA AGENDA
				if nPosDia > 0
					aAdd(HTTPSESSION->AGENDAS[nPosRec][3][nPosDia],{;
						dtoc(stod(oTmp[nx]:cDATESCHE))/*1 data*/,;
						oTmp[nx]:cPROJCODE/*2 prj*/,;
						oTmp[nx]:cPROJTASK/*3 tsk*/,;
						oTmp[nx]:cTIMESTAR/*4 hr ini*/,;
						oTmp[nx]:cTIMEEND/*5 hr fim*/,;
						oTmp[nx]:cPROJDESC/*6 prj desc*/,;
						Substr(oTmp[nx]:cCUSTNAME,1,15)/*7 NOME*/,;
						alltrim(oTmp[nx]:cTASKDESC)/*8 TSK DESC*/,;
						oTmp[nx]:nAGERECNO/*9 RECNO AGENDA*/,;
						oTmp[nx]:cOSGERADA/*10 GEROU OS*/,;
						oTmp[nx]:cDESCRIPT/*11 TEXTO DA OS*/,;
						oTmp[nx]:nVLRALMO/*12 ALMOCO*/,;
						oTmp[nx]:nVLRDESP/*13 KM*/,;
						oTmp[nx]:cOBSDESP/*14 OBS DESP*/,;
						oTmp[nx]:cTIMEDESC/*15 outros / descontos*/;
						})
				endif
			next
		endif
	endif
	
	//HTML
	execInPage(cPagina)
	
	Web Extended End

return(cHtml)
